{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Transfer Coefficient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from scipy.stats.stats import pearsonr\n",
    "from scipy.stats.stats import spearmanr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_stocks = 500\n",
    "np.random.seed(201808)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def standardize_alpha(alpha_vector):\n",
    "    return(alpha_vector - np.mean(alpha_vector))/np.sum(np.abs(alpha_vector))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simulate an alpha vector for a single time period"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#make up data for an alpha vector for a single time period\n",
    "def simulate_alpha_vector():\n",
    "    alpha_vector = np.random.normal(size=num_stocks)\n",
    "    return alpha_vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha_vector = simulate_alpha_vector()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simulate optimized portfolio weights\n",
    "You'll learn about optimization using a risk model and constraints in a later lesson.  For, now, we'll simulate portfolio weight optimization by adding some adjustments to the original alpha vector, and pretending these adjusted values are the portfolio weights that are output by an optimizer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def simulate_optimized_weights(alpha_vector):\n",
    "    standard_alpha_vector = standardize_alpha(alpha_vector)\n",
    "    #add some noise that simulates optimization adjusting the weights.\n",
    "    optimized_weights = standard_alpha_vector + np.random.normal(scale=0.001, size=num_stocks)\n",
    "    return optimized_weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "optimized_weights = simulate_optimized_weights(alpha_vector)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize alpha vector versus optimized weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.scatter(alpha_vector,optimized_weights);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_transfer_coefficient(alpha_vector, optimized_weights):\n",
    "    #TODO calculate the transfer coefficient\n",
    "    transfer_coefficient, pvalue = # ...\n",
    "    return transfer_coefficient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "transfer_coefficient = calculate_transfer_coefficient(alpha_vector, optimized_weights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f\"The transfer coefficient is {transfer_coefficient:.4f}\") "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solution notebook\n",
    "[The solution notebook is here.](transfer_coefficient_solution.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
